exports.DatabaseHelper = Core.Class.singleton(
{
	classname: 'DatabaseHelper',
	
	initialize: function()
	{
		this._userObj = null;
		this._userModel = null;
		this._userJSON = null;
		this._neighborName = null;
		this._isFriend = false;
		
		//this._user = null;
	},
	
	loadUser:function()
	{
		var resource = "User";
		
		var path = GAConfigurations.getGameStateDBPath() + "/" +GAConfigurations.getRecordID()+"/";
		console.log("zaki::::: MY GAME STATE PATH IS "+path);

		this._userJSON = new GAPersistedObject(resource, this.doneLoading.bind(this), path);
	},
	
	myProgress:function (loaded, total) 
	{
		
	},
	
	doneLoading : function(className,error) 
	{
		//console.log("zaki::::: DONE LOADING USER COMPLETE");
	},
	
	deleteUserModel:function()
	{

	},
	
	unloadUser: function()
	{
		this._userJSON.destroy();
		this._userJSON = null;
	},
	
    isValidPurchase: function(currencyType, defaultBuyPrice)
    {
    	var coins = this.getCoins();
    	var nuggets = this.getNuggets();
    	if (currencyType == kCurrencyType.kCurrencyTypeNuggets)
    	{
    		if (+defaultBuyPrice <= +nuggets)
    		{
    			return true;
    		}
    	}
    	else 
    	{
    		if(+defaultBuyPrice <= +coins)
    		{
    			return true;
    		}
    	}
    	return false;
    },	
	
//	duplicateUser: function()
//	{
//		//this._userModel = MMGameState.getCurrentUser();
//		this._userModel = DatabaseInterface.getUserObj();
//	},
	
	getUser: function()
	{
		return this._userJSON;
	},
	
	updateCurrency:function (type, currency)
	{	
		if(currency < 0) {
			currency = 0;
		}
		switch (type) {
			case kOperationType.kOperationAddCoins:
			{
				this._userJSON.coins = this._userJSON.coins + currency;
                break;
			}
			case kOperationType.kOperationSubtractCoins:
			{
				this._userJSON.coins = this._userJSON.coins - currency;
				break;
			}
			case kOperationType.kOperationAddNuggets:
			{	
				this._userJSON.nuggets = this._userJSON.nuggets + currency;
				break;
			}
			case kOperationType.kOperationSubtractNuggets:
			{
				this._userJSON.nuggets = this._userJSON.nuggets - currency;
				break;	
			}
			default:
				break;
		}
		
		if(this._userJSON.coins > MM_MAX_USER_COINS) {
			this._userJSON.coins = MM_MAX_USER_COINS;
		}
		if(this._userJSON.coins <= 0) {
			this._userJSON.coins = 0;
		}
		
		if(this._userJSON.nuggets > MM_MAX_USER_NUGGETS) {
			this._userJSON.nuggets = MM_MAX_USER_NUGGETS;
		}
		if(this._userJSON.nuggets <= 0) {
			this._userJSON.nuggets = 0;
		}		
		this.updateUser(); 
	},
	
	getHasCompletedTutorial: function()
	{
		if(this._userJSON)
		{
			return this._userJSON.hasCompletedTutorial;
		}
		return null;
	},
	
	setHasCompletedtutorial: function(value)
	{
		this._userJSON.hasCompletedTutorial = value;
		this.updateUser();
	},
	
	getCurrentTutorialStep: function()
	{
		if(this._userJSON)
		{
			return this._userJSON.currentTutorialStep;
		}
		return null;
	},
	
	setCurrentTutorialStep: function(value)
	{
		this._userJSON.currentTutorialStep = value;
		this.updateUser();
	},
	
	updateUser: function()
	{
		this._userJSON.persist();
//		var pathToResource = GAConfigurations.getGameStateDBPath() +"/"+GAConfigurations.getUserId()+"/User.json";			
//		var jsonStr = JSON.stringify(this._userJSON);
//		console.log("zaki::::: JSON STRING TO PERSIST IS "+jsonStr);
//		
//		Storage.FileSystem.writeFile(pathToResource, jsonStr);			
	},
	
	getCoins: function()
	{
		return this._userJSON.coins;
	},
	
	getNuggets: function()
	{
		return this._userJSON.nuggets;
	},
	
	getGamerTag: function()
	{
		return this._userJSON.gamerTag;
	},
	
	getNeighborGamerTag: function()// very big chapee for time being 
	{
		return this._neighborGamerTag;
	},
	
	setNeighborGamerTag: function(name)// very big chapee for time being 
	{
		this._neighborGamerTag = name;
	},
	
	destroy: function()
	{
		
	},
});

